<?php
/**  
 * 产品类别增删改查
 * @access public|private|protected
 * @param string xxx
 * @return void|int|string|boolean|array        comment 
 * @author      ZhangLiLi <379417738@qq.com>
 * @version	  v1.0.0 
 * @copyright  2017-1-17 下午5:04:20
*/
namespace Admin\Controller;
use Admin\Controller\AdminController;
class PtypeController extends AdminController {
	public function _initialize(){
		parent::_initialize();
	}
	//空操作
	public function _empty(){
		header("HTTP/1.0 404 Not Found");
		$this->display('Public:404');
	}
    /**
     * 察品类别页显示
     * @author      ZhangLiLi <379417738@qq.com>
     * @version	  v1.0.0 
     * @copyright  2017-1-17 下午5:04:43
     */
	public function index(){
		$where["status"]=array("NEQ","-1");
		$count      = M("Ptype")->where($where)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list=M("Ptype")->where($where)->limit($Page->firstRow.','.$Page->listRows)->order("sort asc")->select();
		$this -> assign('list',$list);
		$this->assign('page',$show);
		$this -> display();
	} 
    /**
     * 添加或修该页显示
     * @author      ZhangLiLi <379417738@qq.com>
     * @version	  v1.0.0 
     * @copyright  2017-1-17 下午5:05:07
     */
	public function insert(){
		$tid=I("tid","");
		if(!empty($tid)){
			$re=M("Ptype")->where(array("tid"=>$tid))->find();
			if($re["pid"]!==0){
				$tlist=M("Ptype")->where(array("tid"=>$re['pid']))->find();
			}
			$this->assign("re",$re);
			$this->assign("tlist",$tlist);
			$but="修改";
			
		}else{
			$but="添加";
		}
		//分类显示
		//查找一级分类
		$where["pid"]=0;
		$where["status"]=array("NEQ","-1");
		$list=M("Ptype")->where($where)->select();
		//以一级分类为基础，形成遍历
		foreach ($list as $k=>$v){
			//加入二级分类，以一级id为查询条件
			$where1["pid"]=$v["tid"];
			$where1["status"]=array("NEQ","-1");
			$list[$k]["children"]=M("Ptype")->where($where1)->select();
		}
		$this->assign("list",$list);
		$this->assign("but",$but);
		$this->display();
	}
    /**
     * 添加或修改
     * @author      ZhangLiLi <379417738@qq.com>
     * @version	  v1.0.0 
     * @copyright  2016-12-22 下午1:17:47
     */
	public function addPtype(){
		$data['tname']=I("tname","");
		$data['pid']=I("pid","");
		$where["tid"]=I("tid","");
		$data["sort"]=I("sort","");
		if(!empty($where["tid"])){
		 	//更改
// 		 	dump($data);die;
	        $re=M('Ptype') -> where($where) ->save($data);
	        $su_str='修改成功';
	        $er_str='修改失败';
		}else{
			//添加
			$data['status']=I("status","0");
			if(empty($data['tname'])){
	        	$this -> error('类别名称不能为空');
	        }
			$re=M('Ptype') ->add($data);
			$su_str='添加成功';
			$er_str='添加失败';
		}
		if($re){
      		$this -> success($su_str,U('Ptype/index'));
      	}else{
      		$this -> error($er_str);
      	}
	}
	/**
	 * 删除
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2016-12-22 上午11:36:15
	 */
    public function delete(){
    	$tid=I("tid")?I("tid"):null;
		if(!empty($tid)){
			$list=M("Ptype")->where(array("pid"=>$tid))->find();
			if($list){
				$this->error("该类别下还有子分类，不能删除");
			}
			$res=M("Ptype")->where(array("tid"=>$tid))->setField("status",-1);
			if($res){
				$this->success("删除成功",U("Ptype/index"));
			}else{
				
				$this->error("删除失败");
			}
		}
    }
	/**
	 * 状态
	 * @author      ZhangLiLi <379417738@qq.com>
	 * @version	  v1.0.0 
	 * @copyright  2016-12-22 下午1:08:51
	 */
	public function open(){
		$where['position_id']=I('position_id')?I('position_id'):null;
		$list=M('Position')->where($where)->find();
		if($list['status']==0){
			$list=M('Position')->where($where)->setField('status','1');
			$this->success('状态修改成功');
		}
		if($list['status']==1){
			$list=M('Position')->where($where)->setField('status','0');
			$this->success('状态修改成功');
		}
	}


}